package com.charles.core.generator;


import com.charles.service.IdBlockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author charles
 * @date 2022/11/6 3:55
 */
@Component
public class IdGeneratorFactoryClient implements IdGeneratorFactory {

    private static final ConcurrentHashMap<String, UIdGenerator> generators = new ConcurrentHashMap<>();

    @Autowired
    private IdBlockService idBlockService;

    @Override
    public UIdGenerator getIdGenerator(String bizType) {
        if (generators.containsKey(bizType)) {
            return generators.get(bizType);
        }
        synchronized (this) {
            if (generators.containsKey(bizType)) {
                return generators.get(bizType);
            }
            UIdGenerator UIdGenerator = new DbIdGenerator(idBlockService, bizType);

            generators.put(bizType, UIdGenerator);
            return UIdGenerator;
        }
    }
}
